home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / admin / kvar < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  5.2 KB  |  222 lines

  1. #!/bin/ksh
  2. # @(#) kvar.ksh 2.1 96/09/07
  3. # 94/05/14 john h. dubois iii
  4. # 94/06/13 Check for bad symbol name.
  5. # 96/01/21 5.0 port.  New crash has different behaviour if input is not a tty
  6. #          and different error messages.
  7. # 96/02/02 Print warnings & read again.  Check values.
  8. # 96/04/23 Added tnmdrsRS options; changed old s option to V.
  9. # 96/09/07 Added bOXDT options.
  10.  
  11. name=${0##*/}
  12. Usage=\
  13. "Usage: $name [-hnxOXDT] [-t<string>] [-s<seconds>] [-r<num>] [-m<mult>]
  14.             [-d<div>] [-b<base>] [-RSV|var-name ...]"
  15. list=
  16. Tabstring="\n"
  17. typeset -i Mult=1 Div=1 Repeat=0 Seconds=0
  18. PrintNames=true
  19. radix=10
  20. printFlag=d
  21. seg=
  22.  
  23. while getopts :hnxm:d:RSVt:r:s:b:OXDT opt; do
  24.     case $opt in
  25.     h)
  26.     echo \
  27. "$name: print the value of kernel integer variables.
  28. $Usage
  29. Options:
  30. -b<base>: Print values using radix <base>, which must be a number in the range
  31.     2 through 36.  The default is 10.
  32. -X: Print values as unsigned hexadecimal values.  Cannot be used with -m or -d.
  33. -O: Print values as unsigned octal values.  Cannot be used with -m or -d.
  34. -D: Print the data symbols nearest to values.
  35. -T: Print the text symbols nearest to values.
  36. -m<mult>: Multiply values by integer value <mult> before printing them.
  37. -d<div>: Divide values by integer value <div> before printing them.
  38.     Values for -m and -d may be given in ksh form (base#value).
  39. -h: Print this help.
  40. -n: Do not print variable names.
  41. -t<string>: Separate each variable display with <string> instead of a newline.
  42. -s<seconds>: Print values every <seconds> seconds.  If -r is not also given,
  43.     values are printed until $name is interrupted.
  44. -r<num>: Print values <num> times.  If -s is not also given, they are printed
  45.     about once per second.
  46.     -r and -s require the \"timer\" utility to be available in the search path.
  47. -x: Turn on debugging.
  48. -R: Print freemem as a value in kilobytes.
  49. -S: Print freeswap as a value in kilobytes.
  50. -V: Print availsmem as a value in kilobytes."
  51.     exit 0
  52.     ;;
  53.     [RSV])
  54.     case $opt in
  55.         R) list="$list freemem";;
  56.         S) list="$list freeswap";;
  57.         V) list="$list availsmem";;
  58.     esac
  59.     Mult=4
  60.     Div=1
  61.     ;;
  62.     b)
  63.     radix=$OPTARG
  64.     if [[ "$OPTARG" != +([0-9]) || "$OPTARG" -lt 2 || "$OPTARG" -gt 36 ]]
  65.     then
  66.         print -ru2 "Bad radix: $radix"
  67.         exit 1
  68.     fi
  69.     ;;
  70.     t)
  71.     Tabstring=$OPTARG
  72.     ;;
  73.     x)    set -x;;
  74.     n)  PrintNames=false;;
  75.     r)
  76.     Repeat=$OPTARG || exit 1
  77.     if [ Repeat -le 0 ]; then
  78.         print -u2 "Must give a positive integer value with $opt."
  79.         exit 1
  80.     fi
  81.     ;;
  82.     s)
  83.     Seconds=$OPTARG || exit 1
  84.     if [ Seconds -le 0 ]; then
  85.         print -u2 "Must give a positive integer value with $opt."
  86.         exit 1
  87.     fi
  88.     ;;
  89.     m)
  90.     Mult=$OPTARG || exit 1
  91.     ;;
  92.     d)
  93.     Div=$OPTARG || exit 1
  94.     if ! ((Div)); then
  95.         print -u2 "Cannot divide by zero."
  96.         exit 1
  97.     fi
  98.     ;;
  99.     O)
  100.     printFlag=o
  101.     ;;
  102.     X)
  103.     printFlag=x
  104.     ;;
  105.     D)
  106.     seg=ds
  107.     printFlag=x
  108.     ;;
  109.     T)
  110.     seg=ts
  111.     printFlag=x
  112.     ;;
  113.     +?)
  114.     print -u2 "$name: options should not be preceded by a '+'."
  115.     exit 1
  116.     ;;
  117.     :)
  118.         print -r -u2 -- \
  119.         "$name: Option '$OPTARG' requires a value.  Use -h for help."
  120.         exit 1
  121.         ;;
  122.     ?) 
  123.     print -u2 "$name: $OPTARG: bad option.  Use -h for help."
  124.     exit 1
  125.     ;;
  126.     esac
  127. done
  128.  
  129. # remove args that were options
  130. let OPTIND=OPTIND-1
  131. shift $OPTIND
  132.  
  133. if [ $# -lt 1 -a -z "$list" ]; then
  134.     print -u2 "$Usage\nUse -h for help."
  135.     exit
  136. fi
  137.  
  138. if [ ! -r /dev/mem ]; then
  139.     print -u2 "Cannot open /dev/mem."
  140.     exit 1
  141. fi
  142.  
  143. function GetValues {
  144.     typeset -L17 pvar
  145.     typeset value
  146.     typeset DoTab=false
  147.     typeset var line sign= pt
  148.     if [ radix -eq 10 ]; then
  149.     typeset out=
  150.     else
  151.     typeset -i$radix out
  152.     fi
  153.  
  154.  
  155.     for var in $list $*; do
  156.     if $DoTab; then
  157.         print -n "$Tabstring"
  158.     else
  159.         DoTab=true
  160.     fi
  161.     print -rp od -$printFlag $var
  162.     read -p line
  163.     # Get rid of header.  Must do it here rather than immediately after
  164.     # starting coprocess because new crash does not print the header until
  165.     # the first line of other output.
  166.     [[ "$line" = dumpfile* ]] && read -p line
  167.     if [[ "$line" = Warning* ]]; then
  168.         print -ru2 -- "$line"
  169.         read -p line
  170.     fi
  171.     if [[ "$line" = *@(not found in symbol table|is an unknown symbol)* ]]
  172.     then
  173.         print -ru2 "$var: No such symbol."
  174.         continue
  175.     fi
  176.     set -- $line
  177.     # Old crash will have a prompt as field 1; new one does not,
  178.     # so do a shift to make the value be in a known place.
  179.     shift $(($#-1))
  180.     if [[ "$1" != +([-0-9a-f]) ]]; then
  181.         print -ru2 "Error getting value of $var:
  182. >> $line"
  183.         continue
  184.     fi
  185.     value=$1
  186.     pvar=$var:
  187.     $PrintNames && print -rn -- "$pvar "
  188.     if [ -n "$seg" ]; then
  189.         print -rp "$seg $value"
  190.         read -p line
  191.         print -nr -- "$line"
  192.     else
  193.         if [ Mult -ne 1 -o Div -ne 1 ]; then
  194.         out=$((value*Mult/Div))
  195.         else
  196.         out=$value
  197.         fi
  198.         if [[ "$out" = -* ]]; then    # deal with sign separately
  199.         sign=-
  200.         out=${out#-}
  201.         fi
  202.         pt=${out#*#}    # get rid of base spec if any
  203.         pt=${pt##+(0)}    # get rid fo leading 0s
  204.         print -n -- $sign$pt
  205.     fi
  206.     done
  207.     print ""
  208. }
  209.  
  210. crash |&
  211. if [ Repeat -gt 1 -o Seconds -ne 0 ]; then
  212.     # Subtract 1 from Repeat because one instance will be done initially.
  213.     ((Repeat)) && rOpt=-r$((Repeat-1)) || rOpt=
  214.     ((Seconds)) || Seconds=1
  215.     # Use timer instead of sleeps to keep the system as quiescent as possible.
  216.     timer -i $rOpt ${Seconds}s | while read; do
  217.     GetValues "$@"
  218.     done
  219. else
  220.     GetValues "$@"
  221. fi
  222.